package cc;

interface Command {
    void execute();
    void undo();
}

class NoCommand implements Command {
    @Override
    public void execute() { }
    @Override
    public void undo() { }
}

class LightReceiver {
    public void on() {
        System.out.println("电灯打开了..");
    }
    public void off() {
        System.out.println("电灯关闭了..");
    }
}


class LightOnCommand implements Command {
    private LightReceiver light;
    public LightOnCommand(LightReceiver light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.on();
    }
    @Override
    public void undo() {
        light.off();
    }
}


class LightOffCommand implements Command {
    private LightReceiver light;
    public LightOffCommand(LightReceiver light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.off();
    }
    @Override
    public void undo() {
        light.on();
    }
}

class TVReceiver {
    public void on() {
        System.out.println("电视机打开了..");
    }
    public void off() {
        System.out.println("电视机关闭了..");
    }
}


class TVOnCommand implements Command {
    private TVReceiver tv;
    public TVOnCommand(TVReceiver tv) {
        this.tv = tv;
    }
    @Override
    public void execute() {
        tv.on();
    }
    @Override
    public void undo() {
        tv.off();
    }
}

class TVOffCommand implements Command {
    private TVReceiver tv;
    public TVOffCommand(TVReceiver tv) {
        this.tv = tv;
    }
    @Override
    public void execute() {
        tv.off();
    }
    @Override
    public void undo() {
        tv.on();
    }
}


class RemoteController {
    private Command[] onCommands;
    private Command[] offCommands;
    private Command undoCommand;
    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }
    public void setCommand(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }
    public void onButtonWasPushed(int no) {
        onCommands[no].execute();
        undoCommand = onCommands[no];
    }
    public void offButtonWasPushed(int no) {
        offCommands[no].execute();
        undoCommand = offCommands[no];
    }
    public void undoButtonWasPushed() {
        undoCommand.undo();
    }
}

public class ml {
    public static void main(String[] args) {
        // 创建电灯的对象（接受者）
        LightReceiver lightReceiver = new LightReceiver();
        // 创建电灯相关的开关命令
        LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
        LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
        // 需要一个遥控器
        RemoteController remoteController = new RemoteController();
        // 给我们的遥控器设置命令, 比如 no = 0 是电灯的开和关的操作
        remoteController.setCommand(0, lightOnCommand, lightOffCommand);

        System.out.println("--------按下灯的开按钮-----------");
        remoteController.onButtonWasPushed(0);
        System.out.println("--------按下灯的关按钮-----------");
        remoteController.offButtonWasPushed(0);
        System.out.println("--------按下撤销按钮-----------");
        remoteController.undoButtonWasPushed();

        System.out.println("=========使用遥控器操作电视机==========");

        TVReceiver tvReceiver = new TVReceiver();
        TVOffCommand tvOffCommand = new TVOffCommand(tvReceiver);
        TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver);
  
        remoteController.setCommand(1, tvOnCommand, tvOffCommand);

        System.out.println("--------按下电视机的开按钮-----------");
        remoteController.onButtonWasPushed(1);
        System.out.println("--------按下电视机的关按钮-----------");
        remoteController.offButtonWasPushed(1);
        System.out.println("--------按下撤销按钮-----------");
        remoteController.undoButtonWasPushed();
    }
}